package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysTable;
import com.ruoyi.system.service.ISysTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysTableColumnMapper;
import com.ruoyi.system.domain.SysTableColumn;
import com.ruoyi.system.service.ISysTableColumnService;

/**
 * 字段描述Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-26
 */
@Service
public class SysTableColumnServiceImpl implements ISysTableColumnService 
{
    @Autowired
    private SysTableColumnMapper sysTableColumnMapper;

    @Autowired
    private ISysTableService sysTableService;

    /**
     * 查询字段描述
     * 
     * @param id 字段描述主键
     * @return 字段描述
     */
    @Override
    public SysTableColumn selectSysTableColumnById(Long id)
    {
        return sysTableColumnMapper.selectSysTableColumnById(id);
    }

    /**
     * 查询字段描述列表
     * 
     * @param sysTableColumn 字段描述
     * @return 字段描述
     */
    @Override
    public List<SysTableColumn> selectSysTableColumnList(SysTableColumn sysTableColumn)
    {
        return sysTableColumnMapper.selectSysTableColumnList(sysTableColumn);
    }

    /**
     * 新增字段描述
     * 
     * @param sysTableColumn 字段描述
     * @return 结果
     */
    @Override
    public int insertSysTableColumn(SysTableColumn sysTableColumn)
    {
        System.out.println("XXXXXXXXXXX" + sysTableColumn.getColumnName());
        SysTableColumn exist = sysTableColumnMapper.selectColumnByName(sysTableColumn.getColumnName(), sysTableColumn.getTableId());
        if(exist != null) {
            return 0; // 已存在
        }

        Integer currMaxSort = sysTableColumnMapper.getMaxSort(sysTableColumn.getTableId());
        if(currMaxSort == null) {
            currMaxSort = 0;
        }
        currMaxSort += 1;
        sysTableColumn.setSort(Long.valueOf(currMaxSort));

        SysTable table = sysTableService.selectSysTableById(sysTableColumn.getTableId());

        // 添加物理字段
        sysTableColumnMapper.addDbColumn(table.getTableName(), sysTableColumn.getColumnName(), sysTableColumn.getColumnComment(), sysTableColumn.getLen());

        sysTableColumn.setCreateTime(DateUtils.getNowDate());
        return sysTableColumnMapper.insertSysTableColumn(sysTableColumn);
    }

    /**
     * 修改字段描述
     * 
     * @param sysTableColumn 字段描述
     * @return 结果
     */
    @Override
    public int updateSysTableColumn(SysTableColumn sysTableColumn)
    {
        sysTableColumn.setUpdateTime(DateUtils.getNowDate());
        return sysTableColumnMapper.updateSysTableColumn(sysTableColumn);
    }

    /**
     * 批量删除字段描述
     *
     * @param ids     需要删除的字段描述主键
     * @param tableId
     * @return 结果
     */
    @Override
    public int deleteSysTableColumnByIds(Long[] ids, Long tableId)  {

        SysTable table = sysTableService.selectSysTableById(tableId);

        for(Long id : ids) {
            SysTableColumn sysTableColumn = sysTableColumnMapper.selectSysTableColumnById(id);
            // 删除逻辑字段
            sysTableColumnMapper.deleteSysTableColumnById(id);
            // 删除物理字段
            sysTableColumnMapper.dropDbColumn(table.getTableName(), sysTableColumn.getColumnName());
        }
        return ids.length;
    }

    /**
     * 删除字段描述信息
     * 
     * @param id 字段描述主键
     * @return 结果
     */
    @Override
    public int deleteSysTableColumnById(Long id)
    {
        return sysTableColumnMapper.deleteSysTableColumnById(id);
    }

}
